import 'dart:io';

import 'package:epidemic_data/data/data.dart';
import 'package:epidemic_data/model/city_info_response.dart';
import 'package:epidemic_data/widgets/scrollable_widget.dart';
import 'package:flutter/material.dart';

import 'package:dio/dio.dart';

class CityTablePage extends StatefulWidget {
  @override
  _CityTablePageState createState() => _CityTablePageState();
}

class _CityTablePageState extends State<CityTablePage> {
  List<ProvinceArray> provinceArrayList = [];
  List<CityArray> cityArrayList = [];
  int? sortColumnIndex;
  bool isAscending = false;

  @override
  void initState() {
    super.initState();
    _getCityArray();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        body: ScrollableWidget(child: buildDataTable()),
      );

  Widget buildDataTable() {
    final columns = ['城市', '总确诊', '总治愈', '总死亡'];

    return Container(
      child: DataTable(
        sortAscending: isAscending,
        sortColumnIndex: sortColumnIndex,
        columns: getColumns(columns),
        rows: getRows(cityArrayList),
      ),
    );
  }

  List<DataColumn> getColumns(List<String> columns) => columns
      .map((String column) => DataColumn(
            label: Text(column),
            onSort: onSort,
          ))
      .toList();

  List<DataRow> getRows(List<CityArray> users) => users.map((CityArray user) {
        final cells = [user.childStatistic, user.totalConfirmed, user.totalCured, user.totalDeath];

        return DataRow(cells: getCells(cells));
      }).toList();

  List<DataCell> getCells(List<dynamic> cells) =>
      cells.map((data) => DataCell(Text('$data'))).toList();

  void onSort(int columnIndex, bool ascending) {
    if (columnIndex == 0) {
      cityArrayList.sort((user1, user2) =>
          compareString(ascending, user1.childStatistic, user2.childStatistic));
    } else if (columnIndex == 1) {
      cityArrayList.sort((user1, user2) =>
          compareInt(ascending, user1.totalConfirmed, user2.totalConfirmed));
    } else if (columnIndex == 2) {
      cityArrayList.sort((user1, user2) =>
          compareInt(ascending, user1.totalCured, user2.totalCured));
    } else if (columnIndex == 3) {
      cityArrayList.sort((user1, user2) =>
          compareInt(ascending, user1.totalDeath, user2.totalDeath));
    }

    setState(() {
      sortColumnIndex = columnIndex;
      isAscending = ascending;
      // _getProvinceArray();
    });
  }

  int compareString(bool ascending, String value1, String value2) =>
      ascending ? value1.compareTo(value2) : value2.compareTo(value1);

  int compareInt(bool ascending, int value1, int value2) =>
      ascending ? value1.compareTo(value2) : value2.compareTo(value1);

  void _getCityArray() async {
    // Options options = Options(headers: {HttpHeaders.authorizationHeader :"APPCODE fc126498c1af4ac4a325a96229286f78"});
    // Dio dio = Dio();
    // print("dio.get");
    // final response = await dio.get(
    //     "http://ncovdata.market.alicloudapi.com/ncov/cityDiseaseInfoWithTrend",
    //     options: options);
    // CityInfoResponse cityInfo = CityInfoResponse.fromJson(response.data);
    CityInfoResponse cityInfo = CityInfoResponse.fromJson(cityInfoFake);
    provinceArrayList = cityInfo.provinceArray;
    cityArrayList = [];
    for (var province in provinceArrayList){
      cityArrayList.addAll(province.cityArray);
    }
  }
}
